<?php
namespace Tbf\Bundle\TestBundle\Tests;

/**
 * assertEquals很多判断相等的条件,符合php的==的语法规则,但是没有用处...
 * assertSame很多判断相等的条件,要求完全一致,在某些情况下太过麻烦...
 */
class AssertEqualTest extends TestBundleTest
{
    /**
     * 各种相等判断实际效果一览...
     * assertEquals和==不完全相同..
     * 在assertEquals上面 123==true 0==null 0==false '123'!=true
     */
    public function testAssertEquals()
    {
        $test_case_list = array(
            array(
                array('1'=>1),array(1=>1),
                '=='=>true,'==='=>true,'assertEquals'=>true,'assertSame'=>true,
            ),
            array(
                array(15=>'a'),array('15'=>'a'),
                '=='=>true,'==='=>true,'assertEquals'=>true,'assertSame'=>true,
            ),
            array(
                array(1=>'1'),array(1=>1),
                '=='=>true,'==='=>false,'assertEquals'=>true,'assertSame'=>false,
            ),
            array(
                array('1'),array(1),
                '=='=>true,'==='=>false,'assertEquals'=>true,'assertSame'=>false,
            ),
            array(
                array('1','2'),array(1,2),
                '=='=>true,'==='=>false,'assertEquals'=>true,'assertSame'=>false,
            ),
            array(
                array(array('a'=>'10'),2),array(array('a'=>10),2),
                '=='=>true,'==='=>false,'assertEquals'=>true,'assertSame'=>false,
            ),
            //assertEquals的坑又出现了...
            array(
                array(array('a'=>0),2),array(array('a'=>false),2),
                '=='=>true,'==='=>false,'assertEquals'=>true,'assertSame'=>false,
            ),
            array(
                array(array('a'=>123123),2),array(array('a'=>true),2),
                '=='=>true,'==='=>false,'assertEquals'=>true,'assertSame'=>false,
            ),
            array(
                array(array('a'=>0),2),array(array('a'=>null),2),
                '=='=>true,'==='=>false,'assertEquals'=>true,'assertSame'=>false,
            ),
            array(
                array(),null,
                '=='=>true,'==='=>false,'assertEquals'=>false,'assertSame'=>false,
            ),
            array(
                false,null,
                '=='=>true,'==='=>false,'assertEquals'=>true,'assertSame'=>false,
            ),
            array(
                '123',123,
                '=='=>true,'==='=>false,'assertEquals'=>true,'assertSame'=>false,
            ),
            array(
                '123',true,
                '=='=>true,'==='=>false,'assertEquals'=>false,'assertSame'=>false,
            ),
            array(
                123,true,
                '=='=>true,'==='=>false,'assertEquals'=>true,'assertSame'=>false,
            ),
        );
        foreach ($test_case_list as $test_case) {
            $this->assertSame($test_case['==='],$test_case[0]===$test_case[1],'=== fail!');
            $this->assertSame($test_case['=='],$test_case[0]==$test_case[1],'== fail!');
            $this->assertSame($test_case['assertEquals'],
                $this->checkAssertEquals($test_case[0],$test_case[1]),'assertEquals fail!');
            $this->assertSame($test_case['assertSame'],
                $this->checkAssertSame($test_case[0],$test_case[1]),'assertSame fail!');
        }
    }
    public function checkAssertEquals($expect,$actual)
    {
        try {
            \PHPUnit_Framework_Assert::assertEquals($expect,$actual);

            return true;
        } catch (\PHPUnit_Framework_ExpectationFailedException $e) {
            return false;
        }
    }
    public function checkAssertSame($expect,$actual)
    {
        try {
            \PHPUnit_Framework_Assert::assertSame($expect,$actual);

            return true;
        } catch (\PHPUnit_Framework_ExpectationFailedException $e) {
            return false;
        }
    }
}
